package com.example.onlinemusic.Controller;

import com.example.onlinemusic.Mapper.LoveMusicMapper;
import com.example.onlinemusic.constants.Constant;
import com.example.onlinemusic.model.LoveMusic;
import com.example.onlinemusic.model.Music;
import com.example.onlinemusic.model.Result;
import com.example.onlinemusic.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RequestMapping("/loveMusic")
@RestController
public class LoveMusicController {
    @Autowired
    private LoveMusicMapper loveMusicMapper;

    @RequestMapping("/likeMusic")
    public Result likeMusic(@RequestParam String id, HttpServletRequest request) {
        int musicId = Integer.parseInt(id);

        HttpSession session = request.getSession(false);
        System.out.println(session.getAttribute(Constant.USERINFO_SESSION_KEY));
        if (session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            log.info("没有登陆");
            return Result.fail("请登陆后操作");
        }
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getUserId();
        log.info("musicId:{},userId:{}", musicId, userId);
        LoveMusic loveMusic = loveMusicMapper.findLoveMusicByMusicIdAndUserId(userId, musicId);
        if (loveMusic != null) {
            return Result.fail("音乐已经被收藏");
        }
        boolean effect = loveMusicMapper.insertLoveMusic(userId, musicId);
        if (effect) {
            return Result.success("收藏成功", effect);
        } else {
            return Result.fail("收藏失败");
        }
    }

    /***
     * 查找喜欢的音乐
     * @param musicName
     * @param request
     * @return
     */
    @RequestMapping("/findLoveMusic")
    public Result findLoveMusic(@RequestParam(required = false) String musicName, HttpServletRequest request) {
        List<Music> musicList = null;
        //检查是否登陆
        HttpSession session = request.getSession(false);
        System.out.println(session.getAttribute(Constant.USERINFO_SESSION_KEY));
        if (session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            log.info("没有登陆");
            return Result.fail("请登陆后操作");
        }
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getUserId();
        if (musicName == null) {
            musicList = loveMusicMapper.findLoveMusicByUserId(userId);
        } else {
            musicList = loveMusicMapper.findLoveMusicByKeyAndUserId(musicName, userId);
        }
        return Result.success("查询到了所有的歌曲信息", musicList);
    }

    @RequestMapping("/deleteLoveMusic")
    public Result deleteLoveMusic(@RequestParam String id, HttpServletRequest request) {
        int musicId = Integer.parseInt(id);
        //检查是否登陆
        HttpSession session = request.getSession(false);
        System.out.println(session.getAttribute(Constant.USERINFO_SESSION_KEY));
        if (session == null || session.getAttribute(Constant.USERINFO_SESSION_KEY) == null) {
            log.info("没有登陆");
            return Result.fail("请登陆后移除");
        }
        User user = (User) session.getAttribute(Constant.USERINFO_SESSION_KEY);
        int userId = user.getUserId();

        int ret = loveMusicMapper.deleteLoveMusic(userId, musicId);
        if (ret == 1) {
            return Result.success("取消收藏成功", true);
        } else {
            return Result.fail("取消收藏失败");

        }
    }
}
